<?php
require_once 'models/TMSProductSitePublisher.inc';
require_once 'pages/TMSItemPage.inc';
require_once 'widgets/TSkinnableWidget.inc';

class TSitePublisherWidget extends TSkinnableWidget {
	protected function checkSource($src){
		return $src instanceof TMSProductSitePublisher;
	}	
	
	public function http_Publish($prodcat = array()){
		$this->Source->PublishItem($this->response->ModelItem,array_keys($prodcat));
	}
	
	public function Css(){
		?>
			div.product-site-publisher {
				border-style:inset;
				border-width:1px;
				padding-left:15px;
			}
			
			div.product-site-publisher div.tree-view {
				overflow:scroll;
				height:400px;
				width:100%;
			}
			
			div.product-site-publisher div.tree-view div.tree-node-subs {
				padding-left:20px;
			}
			
			div.product-site-publisher div.tree-view div.tree-node-title {
				border-bottom-style:dotted;
				border-bottom-width:1px;
				border-bottom-color:gray;
				padding:4px;
			}
			
			div.product-site-publisher button {
				font-size:12pt;
				padding:5px;
				margin-top:5px;
				margin-bottom:5px;
			}			
		<?php
	}
	
	private function _render_node(TCandyItem $node){
		?>
		<div class="tree-node">
			<div class="tree-node-title"><input type="checkbox" name="prodcat[<?php echo $node->ItemId() ?>]"><?php echo $node; ?></div>
			<div class="tree-node-subs">
			<?php
				$c = $node->Contents();
				foreach ($c as $subnode)
					$this->_render_node($subnode); 
			?>
			</div>
		</div>
		<?php
	}
	
	public function defaultOutput(IThemeEngine $engine){
		?>
		<div id="<?php echo $this->Name(); ?>" class="product-site-publisher">
		<?php
		$issue = $this->Source->ItemIssue($this->response->ModelItem);
		if ($issue){
			?>
			<a class="product-page-link" href="<?php echo $this->response->Service()->ItemEditUrl($issue); ?>"><?php echo $issue; ?></a>
			<?php
		} else {
		?>
		<form method="post">
			<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
			<input type="hidden" name="method" value="Publish" />
			<div class="tree-view">
			<?php 
				$cat = $this->Source->Categories();
				foreach ($cat as $node)
					$this->_render_node($node);
			?>
			</div>
			<button onclick="javascript:submit();"><?php __i('Publish'); ?></button>
		</form>
		<script language="javascript">
			
		</script>
		<?php
		}
		?>
		</div>
		<?php 
	}
}